<template>
  <div>
    <!-- 已驳回 4 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="驳回原因"
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel()"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="type == 1 || type == 2 ? rules : {}"
        label-width="80px"
      >
        <el-form-item v-if="type == 1 || type == 2" label="驳回原因" prop="rejected">
          <el-input
            v-input-restrict
            v-model="form.rejected"
            maxlength="100"
            show-word-limit
            :rows="3"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
        <div v-else style="white-space: pre-wrap"
        >{{ form.rejected }}
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" style="display: flex;justify-content: flex-end">
        <el-button v-preventReClick v-if="type == 1 || type == 2" @click="cancel">取 消</el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="submitForm"
          v-if="type == 1 || type == 2"
          >确 定</el-button>
        <el-button
          v-preventReClick
          @click="cancel"
          v-if="type == 3"
          >关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editCourseTemporary, queryCourseTemporary } from "@/api/course/course";
export default {
  props: {
    open: Boolean,
    type: [Number, String],
    propform: {
      type: [Object],
      default: {},
    },
  },
  data() {
    return {
      // 表单参数
      form: {
        rejected: undefined,
      },
      // 表单校验
      rules: {
        rejected: [
            { required: true, message: "驳回原因不能为空", trigger: "blur" },
          ],
      },
    };
  },
  watch: {
    open(e) {
      if (e) {
        if (this.type == 1) {
          this.reset();
        }
        if (this.type == 3) {
          queryCourseTemporary(this.propform.id).then((res) => {
            console.log(res.data);
            this.form = res.data;
          });
        }
      }
    },
  },
  mounted() {},
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.propform));
          data.status = 5;
          data.rejected = this.form.rejected;
          data.framePic =
            data.framePic.length > 0 ? data.framePic.join(",") : "";
          data.teacherIds =
            data.teacherIds.length > 0 ? data.teacherIds.join(",") : "";
          data.teachingPlacePic =
            data.teachingPlacePic.length > 0
              ? data.teachingPlacePic.join(",")
              : "";
          data.workstationIds =
            data.workstationIds.length > 0 ? data.workstationIds.join(",") : "";
          editCourseTemporary(data).then((response) => {
            this.$emit("update:open", false);
            this.$emit("refresh", false);
            this.$modal.msgSuccess("操作成功");
          });
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        rejected: undefined,
      };
      this.resetForm("form");
    },
    cancel() {
      this.$emit("update:open", false);
    },
  },
};
</script>
<style scoped lang="scss"></style>
